﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Teigha.DatabaseServices;
using Teigha.Geometry;
using static SRB.Tg.DrawingStandard.TextPloter;

namespace SRB.Tg.DrawingStandard
{
    public class DimPloter
    {      

        /// <summary>
        /// 线之间角度标注
        /// </summary>
        /// <param name="db"></param>
        /// <param name="L1"></param>
        /// <param name="L2"></param>
        /// <param name="Pref">插入参考点</param>
        /// <param name="scale"></param>
        /// <param name="plotunit"></param>
        /// <param name="paperunit"></param>
        /// <returns></returns>
        public static void DimAng(Database db, Line L1, Line L2, Point3d Pref,ref Extents2d ext, double scale=1,string replaceText="", Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter)
        {
            //DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
            if (scale < 1)
            {
                DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
            }
            else
            {
                DimStyleName += "1-" + scale.ToString();
            }
            LineAngularDimension2 AD1;
           

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead, true);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId dimID = dst[DimStyleName];

                AD1 = new LineAngularDimension2(L1.StartPoint, L1.EndPoint, L2.StartPoint, L2.EndPoint, Pref, replaceText, dimID);
                AD1.LayerId = lt["H标注"];
                AD1.Dimtdec = 1;
                AD1.Dimdec = 2;
                ext = ext.Add(new Extents2d(AD1.Bounds.Value.MinPoint.C2D(), AD1.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(AD1);
                tr.AddNewlyCreatedDBObject(AD1, true);

                tr.Commit();
              
            }
        }



        /// <summary>
        /// 对齐标注
        /// </summary>
        /// <param name="P1">测量起点</param>
        /// <param name="P2">测量</param>
        /// <param name="Pref">插入参考点</param>
        ///  <param name="ext"></param>
        /// <param name="dimID"></param>
        /// <param name="layerName"></param>
        /// <param name="scale"></param>
        /// <param name="plotunit"></param>
        /// <param name="paperunit"></param>
        /// <returns></returns>
        public static void DimAli(Database db, Point3d P1, Point3d P2, Point3d Pref, ref Extents2d ext, double scale = 1, string replaceText = "", Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter,int Dimdec=0, bool isUp=true)
        {
            //DBObjectCollection res = new DBObjectCollection();
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double w = 0, h = 0;
            //Database db = HostApplicationServices.WorkingDatabase;
            MText txt1 = TextPloter.GetTextActualWidth(db, replaceText, scale, out w, out h, 2.5, Extensions.curFont);
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
            AlignedDimension AD1;
            //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                var dimID = dst[DimStyleName];
                AD1 = new AlignedDimension(P1, P2, Pref, replaceText, dimID);
                AD1.LayerId =lt["H标注"];
                double d = AD1.Measurement;
                if (d < w - 10)
                {
                    AD1.DimensionText = "      " + replaceText;
                }
                if (isUp)
                {
                    AD1.Dimtad = 1;
                }
                else
                {
                    AD1.Dimtad = 4;
                 
                }
                if (d < w - 5)
                {
                    int val = (int)((w + d) / (scale * 2.5));
                    string space = "      ";
                    if (val + 1 < 3)
                        space = "   ";
                    else if (val + 1 < 6)
                        space = "       ";
                    else if (val + 1 < 9)
                        space = "         ";
                    else
                        space = "      ";
            
                    AD1.DimensionText = space + replaceText + @"     ";
                    if (P1.X == P2.X)
                        AD1.DimLinePoint = Pref.C3D(0, -w);
                    else if (P1.Y == P2.Y)
                        AD1.DimLinePoint = Pref.C3D(-w);
                    else
                        AD1.DimLinePoint = Pref.C3D();
                }
               
                AD1.Dimdec = Dimdec;
                ext = ext.Add(new Extents2d(AD1.Bounds.Value.MinPoint.C2D(), AD1.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(AD1);
                tr.AddNewlyCreatedDBObject(AD1, true);
                tr.Commit();
              
            }
            
        }

        /// <summary>
        /// 半径标注
        /// </summary>
        /// <param name="Center">圆心</param>
        /// <param name="P1">测量</param>
        /// <param name="Pref">插入参考点</param>
        ///  <param name="ext"></param>
        /// <param name="dimID"></param>
        /// <param name="layerName"></param>
        /// <param name="scale"></param>
        /// <param name="plotunit"></param>
        /// <param name="paperunit"></param>
        /// <returns></returns>
        public static void DimRadius(Database db, Point3d Center, Point3d P1,double leaderLength, ref Extents2d ext, double scale = 1, string replaceText = "", Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter)
        {
            //DBObjectCollection res = new DBObjectCollection();
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
            //AlignedDimension AD1;
            RadialDimension AD1;
                        //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                var dimID = dst[DimStyleName];
                AD1 = new RadialDimension(Center, P1, leaderLength, replaceText, dimID);
                AD1.LayerId = lt["H标注"];
                ext = ext.Add(new Extents2d(AD1.Bounds.Value.MinPoint.C2D(), AD1.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(AD1);
                tr.AddNewlyCreatedDBObject(AD1, true);
                tr.Commit();

            }

        }
        public static void DimRadiusExtend(Database db, Point3d Center, Point3d P1, double leaderLength, ref Extents2d ext, double scale = 1, double extendscale = 1, string replaceText = "", Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter)
        {
            //DBObjectCollection res = new DBObjectCollection();
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
            //AlignedDimension AD1;
            RadialDimension AD1;
            //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1E";
                }
                else
                {
                    DimStyleName += "1E-" + scale.ToString();
                }
                var dimID = dst[DimStyleName];
                AD1 = new RadialDimension(Center, P1, leaderLength, replaceText, dimID);
                AD1.LayerId = lt["H标注"];
                ext = ext.Add(new Extents2d(AD1.Bounds.Value.MinPoint.C2D(), AD1.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(AD1);
                tr.AddNewlyCreatedDBObject(AD1, true);
                tr.Commit();

            }

        }

        /// <summary>
        /// 线性标注
        /// </summary>
        /// <param name="db"></param>
        /// <param name="P1">起点</param>
        /// <param name="P2">终点</param>
        /// <param name="Pref">标注位置</param>
        /// <param name="ext"></param>
        /// <param name="ang">转角，弧度</param>
        /// <param name="scale"></param>
        /// <param name="replaceText"></param>
        /// <param name="pL">合并分段长度（5*100，PL=100）</param>
        /// <param name="tstyle"></param>
        /// <param name="plotunit"></param>
        /// <param name="paperunit"></param>  
        public static void DimRot(Database db  ,Point3d P1, Point3d P2, Point3d Pref, ref Extents2d ext
            , double ang = 0, double scale = 1,  string replaceText = " ",  Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter,int Dimdec=0,bool isAddSpaceRight=true)
        {

            double w = 0, h = 0;
           
            MText txt1 = TextPloter.GetTextActualWidth(db, replaceText, scale, out w, out h, 2.5, Extensions.curFont);
            //Database db = HostApplicationServices.WorkingDatabase;
            //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
            RotatedDimension D1;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                string st = DimStyleName;
              
                var dimID = dst[st];
                Console.WriteLine(replaceText);
                D1 = new RotatedDimension(GeTools.DegreeToRadian(ang)/*ang*/, P1, P2, Pref, replaceText, dimID);
                D1.LayerId = lt["H标注"];
              
                double d = D1.Measurement;
                if(d< w-5)
                {
                    int val = (int)((w+ d) / (scale*2.5));
                    string space = "      ";
                    if (val+1 < 3)
                        space = "   ";
                    else if (val+1 < 6)
                        space = "       ";
                    else if (val+1 < 9)
                        space = "         ";
                    else
                        space = "      ";
                    if (isAddSpaceRight)
                    D1.DimensionText = space + replaceText;
                    else
                        D1.DimensionText = space + replaceText + @"     ";
                }
               
         
                D1.Dimdec = Dimdec;

              
                modelSpace.AppendEntity(D1);
                try
                {
                    ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));
                }
                catch { }
                tr.AddNewlyCreatedDBObject(D1, true);
               
                Console.WriteLine(d);
                tr.Commit();

             
            }
         

        }
        public static void DimRotExtend(Database db, Point3d P1, Point3d P2, Point3d Pref, ref Extents2d ext
      , double ang = 0, double scale = 1, double extendscale = 1, string replaceText = " ", Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter, int Dimdec = 0, bool isAddSpaceRight = true)
        {

            double w = 0, h = 0;

            MText txt1 = TextPloter.GetTextActualWidth(db, replaceText, scale, out w, out h, 2.5, Extensions.curFont);
            //Database db = HostApplicationServices.WorkingDatabase;
            //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
            RotatedDimension D1;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1E";
                }
                else
                {
                    DimStyleName += "1E-" + scale.ToString();
                }
                string st = DimStyleName;

                var dimID = dst[st];
                Console.WriteLine(replaceText);
                D1 = new RotatedDimension(GeTools.DegreeToRadian(ang)/*ang*/, P1, P2, Pref, replaceText, dimID);
                D1.LayerId = lt["H标注"];

                double d = D1.Measurement;
                if (d < w - 5)
                {
                    int val = (int)((w + d) / (scale * 2.5));
                    string space = "      ";
                    if (val + 1 < 3)
                        space = "   ";
                    else if (val + 1 < 6)
                        space = "       ";
                    else if (val + 1 < 9)
                        space = "         ";
                    else
                        space = "      ";
                    if (isAddSpaceRight)
                        D1.DimensionText = space + replaceText;
                    else
                        D1.DimensionText = space + replaceText + @"     ";
                }


                D1.Dimdec = Dimdec;


                modelSpace.AppendEntity(D1);
                try
                {
                    ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));
                }
                catch { }
                tr.AddNewlyCreatedDBObject(D1, true);

                Console.WriteLine(d);
                tr.Commit();


            }


        }

        public static void DimRotNew(Database db, Point3d P1, Point3d P2, Point3d Pref, ref Extents2d ext
     , double ang = 0, double scale = 1, string replaceText = "", Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter,double topH=0, int Dimdec = 0)
        {

            //Database db = HostApplicationServices.WorkingDatabase;
            //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
            RotatedDimension D1;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                string st = DimStyleName;

                var dimID = dst[st];
                Console.WriteLine(replaceText);
                D1 = new RotatedDimension(GeTools.DegreeToRadian(ang)/*ang*/, P1, P2, Pref, replaceText, dimID);
                D1.LayerId = lt["H标注"];
                D1.Dimtvp = topH;
                D1.Dimdec = Dimdec;


                modelSpace.AppendEntity(D1);
                try
                {
                    ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));
                }
                catch { }
                tr.AddNewlyCreatedDBObject(D1, true);
                double d = D1.Measurement;
                Console.WriteLine(d);
                tr.Commit();


            }


        }

        /// <summary>
        /// 直径标注测量(结果：D+直径)
        /// </summary>
        /// <param name="db"></param>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="Pref"></param>
        /// <param name="ext"></param>
        /// <param name="ang"></param>
        /// <param name="scale"></param>
        /// <param name="dimtxtStyle"></param>
        /// <param name="plotunit"></param>
        /// <param name="paperunit"></param>
        /// <param name="unit"></param>
        public static void DimDiameterRotated(Database db, Point3d P1, Point3d P2, Point3d Pref,ref Extents2d ext
            , double ang = 0, double scale = 1,Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter, string unit = "mm")
        {
           
            //Database db = HostApplicationServices.WorkingDatabase;
            //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
            if (scale < 1)
            {
                DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
            }
            else
            {
                DimStyleName += "1-" + scale.ToString();
            }
            RotatedDimension D1;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                var dimID = dst[DimStyleName];

                D1 = new RotatedDimension(GeTools.DegreeToRadian(0)/*ang*/, P1, P2, Pref, "", dimID);
                D1.LayerId = lt["H标注"];
               
                D1.DimensionText = "D" + D1.Measurement.ToString();
                ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(D1);
                tr.AddNewlyCreatedDBObject(D1, true);


                tr.Commit();
             

            }

        }

        private static void AddListDimRotated(Database db,
            Point3d Pref, List<Point3d> npts,ref Extents2d ext,  int scale, double ang = 0, int pN = 1, Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter)
        {
          
            //Database db = HostApplicationServices.WorkingDatabase;
            //ext = new Extents2d(Pref.C2D(), Pref.C2D());
            Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
            {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
            };
            Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
            {
                { Unit.Meter,"M" },
                { Unit.Centimeter,"CM" },
                { Unit.Millimeter,"MM" },
            };
            double factor = UnitDic[plotunit] / UnitDic[paperunit];
            string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);

            RotatedDimension D1 = new RotatedDimension();

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                var dimID = dst[string.Format("1-{0:G}", scale)];

                if (npts.Count > 1)
                {
                    if (pN == 1)
                    {
                        for (int i = 0; i < npts.Count - 1; i++)
                        {
                            Point3d P1 = npts[i];
                            Point3d P2 = npts[i + 1];
                            //Point3d Pref = GeTools.MidPoint(P1, P2).C3D(3, 3);
                            D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, "", dimID);
                            D1.LayerId = lt["H标注"];

                            ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));
                            modelSpace.AppendEntity(D1);
                            tr.AddNewlyCreatedDBObject(D1, true);
                        }
                    }
                    else
                    {
                        if (npts.Count == 2)
                        {
                            Point3d P1 = npts[0];
                            Point3d P2 = npts[1];
                            //Point3d Pref = GeTools.MidPoint(P1, P2).C3D(0, 3);
                            D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, "", dimID);
                            D1.LayerId = lt["H标注"];
                            string replaceText = (D1.Measurement / pN) + "×" + pN;
                            D1.DimensionText = replaceText;

                            ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));
                            modelSpace.AppendEntity(D1);
                            tr.AddNewlyCreatedDBObject(D1, true);
                        }
                    }
                }
                tr.Commit();
             

            }
  
        }


        /// <summary>
        /// 绘制标高符号
        /// </summary>
        /// <param name="bgdata">标高数据</param>
        /// <param name="refpt">标高点</param>
        /// <param name="ext"></param>
        /// <param name="scale">放大倍数</param>
        public static void AddBG(Database db, double bgdata, Point3d refpt, ref Extents2d ext, double scale = 1)
        {
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId blkRecId = blockTbl["BG"];
           
                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    double factor = scale / 100;
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId =lt[ "H标注"];
                    recorder.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);

                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                string stext = string.Format("{0:f3}", bgdata);
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = stext;
                                //acAttRef.Height = acAttRef.Height * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                }
                tr.Commit();
               
            }
          
        }

        public static void DimBG(Database db, string bgdata, Point3d refpt, ref Extents2d ext, double scale = 1)
        {
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId blkRecId = blockTbl["BG"];

                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    double factor = scale / 100;
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId = lt["H标注"];
                    recorder.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);

                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                string stext = bgdata;
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = stext;
                                //acAttRef.Height = acAttRef.Height * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                }
                tr.Commit();

            }

        }
        /// <summary>
        /// 绘制标高符号
        /// </summary>
        /// <param name="bgdata">标高数据</param>
        /// <param name="refpt">标高点</param>
        /// <param name="ext"></param>
        /// <param name="scale">放大倍数</param>
        public static void AddMark(Database db, string mark, Point3d refpt, ref Extents2d ext, double scale = 1,bool isR=false)
        {
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId blkRecId = blockTbl["Mark"];
                if(isR)
                    blkRecId = blockTbl["RMark"];
                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    double factor = scale / 100;
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId = lt["H标注"];
                    recorder.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);

                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                string stext = mark;
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = stext;
                                //acAttRef.Height = acAttRef.Height * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                }
                tr.Commit();

            }

        }

        public static void AddLRMark(Database db, string mark, Point3d refpt, ref Extents2d ext, double scale = 1,bool isTop=true)
        {
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId blkRecId = blockTbl["LRMarkT"];
                if(!isTop)
                    blkRecId = blockTbl["LRMarkB"];

                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    double factor = scale / 100;
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId = lt["H标注"];
                    recorder.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);

                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                string stext = mark;
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = stext;
                                //acAttRef.Height = acAttRef.Height * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                }
                tr.Commit();

            }

        }
        public static void AddLRMarkRotate(Database db, string mark, Point3d refpt, ref Extents2d ext, double scale = 1, bool isTop = true)
        {
            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId blkRecId = blockTbl["LRMarkT"];
                if (!isTop)
                    blkRecId = blockTbl["LRMarkB"];

                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    double factor = scale / 100;
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId = lt["H标注"];
                    acBlkRef.Rotation = GeTools.DegreeToRadian(90);
                    recorder.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);

                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                string stext = mark;
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = stext;
                                acAttRef.Rotation = GeTools.DegreeToRadian(90);
                                //acAttRef.Height = acAttRef.Height * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                }
                tr.Commit();

            }

        }

        /// <summary>
        /// 绘制上下标记符号
        /// </summary>
        /// <param name="bJdata">标记信息</param>
        /// <param name="refpt">标记点</param>
        /// <param name="ext"></param>
        /// <param name="scale"></param>
        /// <param name="textHeight"></param>
        public static void AddL2RBP(Database db, string bJdata, Point3d refpt, ref Extents2d ext, double scale = 1, double textHeight = 3, bool isRight = true)
        {

            //Database db = HostApplicationServices.WorkingDatabase;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                ObjectId blkRecId = blockTbl["RightP"];
                if (!isRight)
                    blkRecId = blockTbl["LeftP"];
                double factor = scale / 100;

                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId = lt["H标注"];
                    recorder.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);
                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = bJdata;
                                //acAttRef.Height = 2;
                                //acAttRef.Height = textHeight * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                }
                tr.Commit();


            }

        }


        /// <summary>
        /// 绘制上下标记符号
        /// </summary>
        /// <param name="bJdata">标记信息</param>
        /// <param name="refpt">标记点</param>
        /// <param name="ext"></param>
        /// <param name="scale"></param>
        /// <param name="textHeight"></param>
        public static void AddTop2BottomBJ(Database db, string bJdata, Point3d refpt, ref Extents2d ext, double scale = 1, double textHeight = 3, bool isTop = true)
        {
           
            //Database db = HostApplicationServices.WorkingDatabase;
  
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                ObjectId blkRecId = blockTbl["TopMark"];
                if (!isTop)
                    blkRecId = blockTbl["BottomMark"];
                double factor = scale / 100;
             
                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId = lt["H标注"];
                    recorder.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);
                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = bJdata;
                                //acAttRef.Height = 2;
                                //acAttRef.Height = textHeight * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                }
                tr.Commit();
               

            }

        }

        /// <summary>
        ///   绘制左右标记符号
        /// </summary>
        /// <param name="bJdata">标记信息</param>
        /// <param name="refpt">标记点</param>
        /// <param name="ext"></param>
        /// <param name="s"></param>
        /// <param name="textHeight"></param>
        /// <param name="isLeft"></param>
        public static void AddLeft2RightBJ(Database db, string bJdata, Point3d refpt, ref Extents2d ext, double scale = 1, double textHeight = 3, bool isLeft = true)
        {
            
            //Database db = HostApplicationServices.WorkingDatabase;
            double factor = scale / 100;//默认初始放大100倍
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId blkRecId = blockTbl["LeftMark"];
                if (!isLeft)
                    blkRecId = blockTbl["RightMark"];
              
                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId = lt["H标注"];
                    modelSpace.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);
                   

                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = bJdata;
                                //acAttRef.Height = 2;
                                //acAttRef.Height = textHeight * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);

                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }
                }
                tr.Commit();
              
            }
            
        }


        /// <summary>
        ///   绘制左右标记符号
        /// </summary>
        /// <param name="bJdata">标记信息</param>
        /// <param name="refpt">标记点</param>
        /// <param name="ext"></param>
        /// <param name="s"></param>
        /// <param name="textHeight"></param>
        /// <param name="isLeft"></param>
        public static void AddLeft2RightBJWithAng(Database db, string bJdata, Point3d refpt, ref Extents2d ext, double scale = 1, double textHeight = 3,double ang=0, bool isLeft = true)
        {

            //Database db = HostApplicationServices.WorkingDatabase;
            double factor = scale / 100;//默认初始放大100倍
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                ObjectId blkRecId = blockTbl["LeftMark"];
                if (!isLeft)
                    blkRecId = blockTbl["RightMark"];
                
                using (BlockReference acBlkRef = new BlockReference(refpt, blkRecId))
                {
                    //acBlkRef.SetAttributes();
                    acBlkRef.ScaleFactors = new Scale3d(factor);
                    acBlkRef.LayerId = lt["H标注"];
                    acBlkRef.Rotate(refpt, ang);
                    modelSpace.AppendEntity(acBlkRef);
                    tr.AddNewlyCreatedDBObject(acBlkRef, true);


                    BlockTableRecord zheshiyuankuai;
                    zheshiyuankuai = tr.GetObject(blkRecId, OpenMode.ForRead) as BlockTableRecord;
                    foreach (ObjectId gezhongshuxingID in zheshiyuankuai)
                    {
                        DBObject gezhongshuxing = tr.GetObject(gezhongshuxingID, OpenMode.ForRead) as DBObject;
                        if (gezhongshuxing is AttributeDefinition)
                        {
                            AttributeDefinition acAtt = gezhongshuxing as AttributeDefinition;
                            using (AttributeReference acAttRef = new AttributeReference())
                            {
                                acAttRef.SetAttributeFromBlock(acAtt, acBlkRef.BlockTransform);
                                acAttRef.Position = acAtt.Position.TransformBy(acBlkRef.BlockTransform);
                                acAttRef.TextString = bJdata;
                                //acAttRef.Height = 2;
                                //acAttRef.Height = textHeight * factor;
                                acBlkRef.AttributeCollection.AppendAttribute(acAttRef);
                                //acAttRef.Rotate(acAttRef.Position,ang);
                                tr.AddNewlyCreatedDBObject(acAttRef, true);
                             
                                ext = ext.Add(new Extents2d(acBlkRef.Bounds.Value.MinPoint.C2D(), acBlkRef.Bounds.Value.MaxPoint.C2D()));
                            }
                        }
                    }

                   
                }
               
                tr.Commit();

            }

        }

        /// <summary>
        /// （水平或垂直）剖断线
        /// </summary>
        /// <param name="stPoint">起点</param>
        /// <param name="endPoint">终点</param>
        /// <param name="ext"></param>
        /// <param name="scale">比例尺</param>
        /// <param name="layer">图层</param>
        public static void AddBreakLine(Database db, Point2d stPoint, Point2d endPoint,ref Extents2d ext, double scale = 1, string layer = "H标注")
        {
            //DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            Polyline line = new Polyline() { Closed = false, Layer = layer, Plinegen = true };//定义不封闭的Polyline 平面虚线
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                //BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                BlockTableRecord recorder;
                recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                #region
                Point2d centerPt = GeTools.MidPoint(stPoint.C3D(), endPoint.C3D()).C2D();
                //画线

                if (stPoint.Y > endPoint.Y && stPoint.X == endPoint.X)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(0, 1.7941 * scale), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(-2.4148 * scale, 1.147 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(2.4148 * scale, -1.147 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(0, -1.7941 * scale), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else if (stPoint.Y <= endPoint.Y && stPoint.X == endPoint.X)
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(0, 1.7941 * scale), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(-2.4148 * scale, 1.147 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(2.4148 * scale, -1.147 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(0, -1.7941 * scale), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }

                if (stPoint.X > endPoint.X && stPoint.Y == endPoint.Y)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.7941 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1.147 * scale, -2.4148 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1.147 * scale, 2.4148 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.7941 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else if (stPoint.X <= endPoint.X && stPoint.Y == endPoint.Y)
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.7941 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1.147 * scale, -2.4148 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1.147 * scale, 2.4148 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.7941 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                tr.Commit();
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                #endregion
            }
            
        }

        /// <summary>
        /// 绘制水平剖断线
        /// </summary>
        /// <param name="stPoint"></param>
        /// <param name="endPoint"></param>
        /// <param name="ext"></param>
        /// <param name="scale">线性比例</param>
        /// <param name="layer">图层</param>
        private static void AddUpperAndDownBreakLine(Database db, Point2d stPoint, Point2d endPoint,ref Extents2d ext, double scale = 20, string layer = "H标注")
        {
           
            Polyline line = new Polyline() { Closed = false, Layer = layer, Plinegen = true };//定义不封闭的Polyline 平面虚线
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                     OpenMode.ForWrite) as BlockTableRecord;
              
                #region
                Point2d centerPt = GeTools.MidPoint(stPoint.C3D(), endPoint.C3D()).C2D();
                //画线
                if (stPoint.X > endPoint.X)
                {
                    line.AddVertexAt(0, stPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.7941 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1.147 * scale, -2.4148 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1.147 * scale, 2.4148 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.7941 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(6, endPoint, 0, 0, 0);
                }
                else
                {
                    line.AddVertexAt(0, endPoint, 0, 0, 0);
                    line.AddVertexAt(1, centerPt.C2D(1.7941 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(2, centerPt.C2D(1.147 * scale, -2.4148 * scale), 0, 0, 0);
                    line.AddVertexAt(3, centerPt, 0, 0, 0);
                    line.AddVertexAt(4, centerPt.C2D(-1.147 * scale, 2.4148 * scale), 0, 0, 0);
                    line.AddVertexAt(5, centerPt.C2D(-1.7941 * scale, 0), 0, 0, 0);
                    line.AddVertexAt(6, stPoint, 0, 0, 0);
                }
               
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                tr.Commit();
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                #endregion
            }
           
        }


        /// <summary>
        /// 增加引线
        /// </summary>
        /// <param name="anchor">标注点</param>
        /// <param name="textAnchor">文字左上角点</param>
        /// <param name="cont">文字内容</param>
        /// <param name="ext"></param>
        /// <param name="scale">比例</param>
        public static void AddMLeader(Database db, Point2d anchor, Point2d textAnchor, string cont,ref Extents2d ext,double scale)
        {
            //Database db = HostApplicationServices.WorkingDatabase;
           
            MLeader mleader = new MLeader();

            string DimStyleName = "ML";
            if (scale < 1)
            {
                DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
            }
            else
            {
                DimStyleName += "1-" + scale.ToString();
            }

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary mlstyles = (DBDictionary)tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead);
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                mleader.MLeaderStyle = (ObjectId)mlstyles[DimStyleName];
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                     OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                mleader.MText = new MText()
                {
                    Contents = cont.Replace("*","×"),
                    Location = textAnchor.C3D(),

                    TextStyleId = st[Extensions.curFont],
                    TextHeight = 2 ,
                    LayerId = lt["H标注"],
                    LineSpacingStyle = LineSpacingStyle.Exactly,
                };

                mleader.MText.LineSpaceDistance = 2;
                mleader.MText.Attachment = AttachmentPoint.BottomLeft;
               
                int idx = mleader.AddLeaderLine(anchor.C3D());

                mleader.LayerId = lt["H标注"];
                modelSpace.AppendEntity(mleader);
                tr.AddNewlyCreatedDBObject(mleader, true);
                ext = ext.Add(new Extents2d(mleader.Bounds.Value.MinPoint.C2D(), mleader.Bounds.Value.MaxPoint.C2D()));
        
                tr.Commit();
             
            }
          
        }

        /// <summary>
        /// 增加引线
        /// </summary>
        /// <param name="anchor">标注点</param>
        /// <param name="textAnchor">文字左上角点</param>
        /// <param name="cont">文字内容</param>
        /// <param name="ext"></param>
        /// <param name="scale">比例</param>
        public static void AddMultiMLeader(Database db, Point2d anchor, Point2d anchor1, Point2d textAnchor, string cont, ref Extents2d ext, double scale)
        {
            //Database db = HostApplicationServices.WorkingDatabase;

            MLeader mleader = new MLeader();

            string DimStyleName = "ML";
            if (scale < 1)
            {
                DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
            }
            else
            {
                DimStyleName += "1-" + scale.ToString();
            }

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                DBDictionary mlstyles = (DBDictionary)tr.GetObject(db.MLeaderStyleDictionaryId, OpenMode.ForRead);
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                mleader.MLeaderStyle = (ObjectId)mlstyles[DimStyleName];
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                     OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                mleader.MText = new MText()
                {
                    Contents = cont.Replace("*", "×"),
                    Location = textAnchor.C3D(),

                    TextStyleId = st[Extensions.curFont],
                    TextHeight = 2,
                    LayerId = lt["H标注"],
                    LineSpacingStyle = LineSpacingStyle.Exactly,
                };

                mleader.MText.LineSpaceDistance = 2;
                mleader.MText.Attachment = AttachmentPoint.BottomLeft;

                int idx = mleader.AddLeaderLine(anchor.C3D());

                mleader.LayerId = lt["H标注"];
                modelSpace.AppendEntity(mleader);
                tr.AddNewlyCreatedDBObject(mleader, true);
                ext = ext.Add(new Extents2d(mleader.Bounds.Value.MinPoint.C2D(), mleader.Bounds.Value.MaxPoint.C2D()));
            
                Line line=  new Line(anchor1.C3D(), textAnchor.C3D(0, -2.67));
                line.LayerId = lt["H标注"];
                modelSpace.AppendEntity(line);
                tr.AddNewlyCreatedDBObject(line, true);
                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));

                tr.Commit();

            }

        }

        /// <summary>
        /// 弧形标注（左边为单弧）
        /// </summary>
        /// <param name="insertPt">插入点（左右两边的中心点）</param>
        /// <param name="lineWidth">左右弧的总宽度</param>
        /// <param name="ext"></param>
        /// <param name="arcAngle">弧形角度</param>
        /// <param name="degree">旋转角度</param>
        public static void ArcUpBreakLine(Database db, Point2d insertPt, double lineWidth,ref Extents2d ext, double arcAngle = 45, double degree = 0)
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // 计算圆弧半径
                double arcRadius = Math.Abs(lineWidth / 4 / Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)));
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                Arc arc1 = new Arc(insertPt.C3D(-arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)), arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(270- arcAngle/2), GeTools.DegreeToRadian(270 + arcAngle/2))
                { LayerId = lt["H标注"] };

                Arc arc2 = new Arc(insertPt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), -(arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2)))),
                    arcRadius, GeTools.DegreeToRadian(90- arcAngle / 2), GeTools.DegreeToRadian(90 + arcAngle / 2))
                { LayerId = lt["H标注"] };

                Arc arc3 = new Arc(insertPt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(270 - arcAngle / 2), GeTools.DegreeToRadian(270 + arcAngle / 2))
                { LayerId = lt["H标注" ]};

                res.Add(arc1);
                res.Add(arc2);
                res.Add(arc3);

                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(degree), Vector3d.ZAxis, insertPt.C3D());
                foreach (DBObject item in res)
                {
                    Entity pr = (Entity)item;
                    pr.TransformBy(TX1);
                    modelSpace.AppendEntity(pr);
                    tr.AddNewlyCreatedDBObject(pr, true);
                    ext = ext.Add(new Extents2d(pr.Bounds.Value.MinPoint.C2D(), pr.Bounds.Value.MaxPoint.C2D()));
                }
                tr.Commit();
             
            }
        }

        /// <summary>
        /// 弧形标注（左边为双弧）
        /// </summary>
        /// <param name="insertPt">插入点（左右两边的中心点）</param>
        /// <param name="lineWidth">左右弧的总宽度</param>
        /// <param name="ext"></param>
        /// <param name="arcAngle">弧形角度</param>
        /// <param name="degree">旋转角度</param>
        public static void ArcBottomBreakLine(Database db, Point2d insertPt, double lineWidth, ref Extents2d ext,double arcAngle = 45, double degree = 0)
        {
            DBObjectCollection res = new DBObjectCollection();
            //Database db = HostApplicationServices.WorkingDatabase;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                // 计算圆弧半径
                double arcRadius = Math.Abs(lineWidth / 4 / Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)));
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                Arc arc1 = new Arc(insertPt.C3D(-arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2)), -arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(90 - arcAngle / 2), GeTools.DegreeToRadian(90 + arcAngle / 2))
                { LayerId = lt["H标注"] };

                Arc arc2 = new Arc(insertPt.C3D((arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(270 - arcAngle / 2), GeTools.DegreeToRadian(270 + arcAngle / 2))
                { LayerId = lt["H标注" ]};

                Arc arc3 = new Arc(insertPt.C3D((-arcRadius * Math.Sin(GeTools.DegreeToRadian(arcAngle / 2))), arcRadius * Math.Cos(GeTools.DegreeToRadian(arcAngle / 2))),
                    arcRadius, GeTools.DegreeToRadian(270 - arcAngle / 2), GeTools.DegreeToRadian(270 + arcAngle / 2))
                { LayerId = lt["H标注"] };

                res.Add(arc1);
                res.Add(arc2);
                res.Add(arc3);

                var TX1 = Matrix3d.Rotation(GeTools.DegreeToRadian(degree), Vector3d.ZAxis, insertPt.C3D());
                foreach (DBObject item in res)
                {
                    Entity pr = (Entity)item;
                    pr.TransformBy(TX1);
                    modelSpace.AppendEntity(pr);
                    tr.AddNewlyCreatedDBObject(pr, true);
                    ext = ext.Add(new Extents2d(pr.Bounds.Value.MinPoint.C2D(), pr.Bounds.Value.MaxPoint.C2D()));
                }
                tr.Commit();
                //return res;
            }
        }

        /// <summary>
        /// 增加方向指向标识()
        /// </summary>
        /// <param name="OrigenPoint">插入点</param>
        /// <param name="text">方向指向地名称</param>
        /// <param name="ext"></param>
        /// <param name="height">文本高度</param>
        /// <param name="scale">比例尺</param>
        /// <param name="isLeft">朝左朝右</param>
        /// <param name="TextStyle">文本样式</param>
        /// <param name="layer">图层</param>
        public static void AddArrowTitle(Database db, Point3d OrigenPoint, string text,ref Extents2d ext, double height = 2.5, double scale = 1, bool isLeft = false, string TextStyle = "SRB.HT", string layer = "H标注")
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {

                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                DBText num = new DBText()
                {
                    Height = height * scale,
                    TextStyleId = st[TextStyle],
                    LayerId = lt[layer],
                    TextString = text,
                    WidthFactor = st[TextStyle].GetXScale(),
                    HorizontalMode = TextHorizontalMode.TextCenter,
                    VerticalMode = TextVerticalMode.TextBase,
                    //AlignmentPoint = insertPt.C3D(0, 2 * _scale),
                };
                //MText num = new MText();
                //num.Contents = text;
                //num.TextHeight = height*scale;
                //double w = num.ActualWidth;// height * 1.25 * text.Length / (0.5 * 2.5*scal);
                double w = num.GetWidth();
                if (isLeft)
                {
                    //OrigenPoint = OrigenPoint.C3D(-w - 5 * scale);
                    //num.AlignmentPoint = OrigenPoint.C3D(3 * scale, 5 * scale);
                    num.AlignmentPoint = OrigenPoint.C3D(3 * scale +w * 0.5, 1 * scale);
                }
                else
                {
                    //OrigenPoint = OrigenPoint.C3D(w + 5 * scale);
                    //num.AlignmentPoint = OrigenPoint.C3D(-3 * scale - w * 0.75, 5 * scale);
                    num.AlignmentPoint = OrigenPoint.C3D(-3 * scale - w*0.5,1*scale);
                }



         
              
                //modelSpace.AppendEntity(num);
                //tr.AddNewlyCreatedDBObject(num, true);
                modelSpace.AppendEntity(num);
                tr.AddNewlyCreatedDBObject(num, true);
                //ext = ext.Add(new Extents2d(num.Bounds.Value.MinPoint.C2D(), num.Bounds.Value.MaxPoint.C2D()));

                Polyline Paa0 = new Polyline() { Closed = true };
                if (isLeft)
                {
                    Paa0.AddVertexAt(0, OrigenPoint.C2D(0, 0), 0, 0, 0);

                    Paa0.AddVertexAt(1, OrigenPoint.C2D(2 * scale, 1.5 * scale), 0, 0, 0);
                    Paa0.AddVertexAt(2, OrigenPoint.C2D(2 * scale, 0.5 * scale), 0, 0, 0);

                    Paa0.AddVertexAt(3, OrigenPoint.C2D(3 * scale + w, 0.5 * scale), 0, 0, 0);
                    Paa0.AddVertexAt(4, OrigenPoint.C2D(3 * scale + w, -0.5 * scale), 0, 0, 0);
                    Paa0.AddVertexAt(5, OrigenPoint.C2D(2 * scale, -0.5 * scale), 0, 0, 0);
                    Paa0.AddVertexAt(6, OrigenPoint.C2D(2 * scale, -1.5 * scale), 0, 0, 0);
                }
                else
                {
                    Paa0.AddVertexAt(0, OrigenPoint.C2D(0, 0), 0, 0, 0);

                    Paa0.AddVertexAt(1, OrigenPoint.C2D(-2 * scale, 1.5 * scale), 0, 0, 0);
                    Paa0.AddVertexAt(2, OrigenPoint.C2D(-2 * scale, 0.5 * scale), 0, 0, 0);

                    Paa0.AddVertexAt(3, OrigenPoint.C2D(-3 * scale - w, 0.5 * scale), 0, 0, 0);
                    Paa0.AddVertexAt(4, OrigenPoint.C2D(-3 * scale - w, -0.5 * scale), 0, 0, 0);
                    Paa0.AddVertexAt(5, OrigenPoint.C2D(-2 * scale, -0.5 * scale), 0, 0, 0);
                    Paa0.AddVertexAt(6, OrigenPoint.C2D(-2 * scale, -1.5 * scale), 0, 0, 0);
                }


                Paa0.LayerId = lt[layer];
               
                modelSpace.AppendEntity(Paa0);
                tr.AddNewlyCreatedDBObject(Paa0, true);
                ext = ext.Add(new Extents2d(Paa0.Bounds.Value.MinPoint.C2D(), Paa0.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            
        }

        /// <summary>
        /// 添加带线的圆文本
        /// </summary>
        /// <param name="textstring">圆文本</param>
        /// <param name="PositionPoint">起点</param>
        /// <param name="endP">终点</param>
        /// <param name="ext"></param>
        /// <param name="isTop">圆在线上部或者线下部</param>
        /// <param name="TextStyle"></param>
        /// <param name="scale"></param>
        /// <param name="txtHeight"></param>
        /// <param name="cR"></param>
        public static void AddLineCirText(Database db, string textstring, Point2d PositionPoint, Point2d endP,ref Extents2d ext, bool isTop = true, string TextStyle = "SRB.HT", double scale = 1, double txtHeight = 2.5, double cR = 3)
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            //DBObjectCollection res = new DBObjectCollection();
            DBText txt = new DBText();
            Circle C1 = new Circle();
            Line L1, L2;
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                txt.TextString = textstring.ToString();
                txt.Height = txtHeight * scale;
                txt.Position = PositionPoint.C3D();
                txt.HorizontalMode = TextHorizontalMode.TextCenter;
                txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                txt.AlignmentPoint = PositionPoint.C3D();
                txt.TextStyleId = st[TextStyle];
                txt.LayerId = lt["H标注"];
                txt.WidthFactor = st[TextStyle].GetXScale();

                C1 = new Circle(PositionPoint.C3D(), Vector3d.ZAxis, cR * scale);
                C1.LayerId =lt["H细线"];

                if(isTop)
                   L1 = new Line(PositionPoint.C3D(0, -3 * scale), endP.C3D());
                else
                   L1 = new Line(PositionPoint.C3D(0, 3 * scale), endP.C3D());
                L1.LayerId = lt["H虚线"];

                //res.Add(txt);
                //res.Add(C1);
                //res.Add(L1);
                modelSpace.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);
                ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.C2D(), txt.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(C1);
                tr.AddNewlyCreatedDBObject(C1, true);
                ext = ext.Add(new Extents2d(C1.Bounds.Value.MinPoint.C2D(), C1.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(L1);
                tr.AddNewlyCreatedDBObject(L1, true);
                ext = ext.Add(new Extents2d(L1.Bounds.Value.MinPoint.C2D(), L1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            
        }

        /// <summary>
        /// 添加外包圆文本
        /// </summary>
        /// <param name="textstring">文本内容</param>
        /// <param name="PositionPoint">插入点</param>
        /// <param name="ext"></param>
        /// <param name="scale">比例尺</param>
        /// <param name="TextStyle">样式</param>
        /// <param name="ang">角度</param>
        /// <param name="txtHeight"></param>
        /// <param name="cR"></param>    
        public static void AddCirText(Database db, string textstring, Point2d PositionPoint,ref Extents2d ext, double scale = 1,string TextStyle = "SRB.HT", double ang = 0, double txtHeight = 2.5, double cR = 2.7)
        {
            //Database db = HostApplicationServices.WorkingDatabase;
            //DBObjectCollection res = new DBObjectCollection();
            DBText txt = new DBText();         
            Circle C2 = new Circle();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;

                txt.TextString = textstring.ToString();
                txt.Height = txtHeight * scale;
                txt.Position = PositionPoint.C3D();
                txt.HorizontalMode = TextHorizontalMode.TextCenter;
                txt.VerticalMode = TextVerticalMode.TextVerticalMid;
                txt.AlignmentPoint = PositionPoint.C3D();
                txt.Rotation =GeTools.DegreeToRadian(ang);
                txt.TextStyleId = st[TextStyle];
                txt.LayerId = lt["H标注"];
                txt.WidthFactor = st[TextStyle].GetXScale();


                C2 = new Circle(PositionPoint.C3D(), Vector3d.ZAxis, cR * scale);
                C2.LayerId = lt["H标注"];

                modelSpace.AppendEntity(txt);
                tr.AddNewlyCreatedDBObject(txt, true);
                ext = ext.Add(new Extents2d(txt.Bounds.Value.MinPoint.C2D(), txt.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(C2);
                tr.AddNewlyCreatedDBObject(C2, true);
                ext = ext.Add(new Extents2d(C2.Bounds.Value.MinPoint.C2D(), C2.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
            }
            //return res;
        }

        /// <summary>
        /// 圆形文字标记
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="point"></param>
        /// <param name="txt"></param>
        /// <param name="tstyle"></param>
        /// <param name="scale"></param>
        public static DBObjectCollection CircularMark(Database db, ref Extents2d ext, Point3d point, string txt, string tstyle = "En", double scale = 100, double Rotation = 0)
        {
            tstyle = "SRB.HT";
            double cr = 2.5;
            DBObjectCollection res = new DBObjectCollection();
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                TextStyleTable st = tr.GetObject(db.TextStyleTableId, OpenMode.ForRead) as TextStyleTable;
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord recorder = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                // 圆内文字
                DBText Mtext = new DBText() { LayerId = lt["H标注"]};
                Mtext.Height = 2.5 * scale;
                Mtext.TextString = txt.ToString();
                Mtext.TextStyleId = st[tstyle];
                Mtext.WidthFactor = 0.8;
                Mtext.Rotation = Rotation;
                cr = Mtext.Height;
                Mtext.HorizontalMode = TextHorizontalMode.TextCenter;
                Mtext.VerticalMode = TextVerticalMode.TextVerticalMid;
                Mtext.AlignmentPoint = point.C3D(cr);
                Mtext.Position = point.C3D(cr);
                ext = ext.Add(new Extents2d(Mtext.Bounds.Value.MinPoint.C2D(), Mtext.Bounds.Value.MaxPoint.C2D()));
                res.Add(Mtext);

                recorder.AppendEntity((Entity)Mtext);
                tr.AddNewlyCreatedDBObject(Mtext, true);

                // 圆
                Circle a = new Circle(point.C3D(cr), Vector3d.ZAxis, cr);
                a.LayerId = lt["H标注"];
                ext = ext.Add(new Extents2d(a.Bounds.Value.MinPoint.C2D(), a.Bounds.Value.MaxPoint.C2D()));
                res.Add(a);
                recorder.AppendEntity((Entity)a);
                tr.AddNewlyCreatedDBObject(a, true);

                tr.Commit();
            }

            return res;
        }

     
        public static void AddListDimRotated(Database db, ref Extents2d ext,
          Point3d Pref, List<Point3d> npts, double scale, double ang = 0, int pN = 1, Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter)
        {

            RotatedDimension D1;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                //var dimID = dst[string.Format("1-{0:G}", scale)];
                Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
               {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
               };
                Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
                {
                   { Unit.Meter,"M" },
                   { Unit.Centimeter,"CM" },
                   { Unit.Millimeter,"MM" },
                 };
                double factor = UnitDic[plotunit] / UnitDic[paperunit];
                string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                var dimID = dst[DimStyleName];
                if (npts.Count > 1)
                {
                    if (pN == 1)
                    {
                        for (int i = 0; i < npts.Count - 1; i++)
                        {
                            Point3d P1 = npts[i];
                            Point3d P2 = npts[i + 1];

                            D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, "", dimID);
                            D1.LayerId = lt["H标注"];

                            modelSpace.AppendEntity(D1);
                            tr.AddNewlyCreatedDBObject(D1, true);
                            Polyline line = new Polyline();
                            line.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                            line.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                            line.AddVertexAt(1, P1.C2D(8 * scale, 8 * scale), 0, 0, 0);
                            line.AddVertexAt(2, P2.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                            if (line.Bounds != null)
                                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                        }
                    }
                    else
                    {
                        if (npts.Count == 2)
                        {
                            Point3d P1 = npts[0];
                            Point3d P2 = npts[1];
                            //Point3d Pref = GeTools.MidPoint(P1, P2).C3D(0, 3);
                            D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, "", dimID);
                            D1.LayerId = lt["H标注"];
                          

                            modelSpace.AppendEntity(D1);
                            //string replaceText = (D1.Measurement / pN) + "×" + pN;
                            string replaceText = (D1.Measurement / (pN * factor)) + "×" + (pN * factor);
                            D1.DimensionText = replaceText;
                            tr.AddNewlyCreatedDBObject(D1, true);
                            //if( D1.Bounds !=null)
                            //ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));
                            //Line line = new Line(P1.C3D(18 * scale, 18 * scale), P2.C3D(-18 * scale, -18 * scale));
                            Polyline line = new Polyline();
                            line.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                            line.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                            line.AddVertexAt(1, P1.C2D(8 * scale, 8 * scale), 0, 0, 0);
                            line.AddVertexAt(2, P2.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                            if (line.Bounds != null)
                                ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                        }
                    }
                }
                tr.Commit();
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="db"></param>
        /// <param name="ext"></param>
        /// <param name="Pref"></param>
        /// <param name="npts"></param>
        /// <param name="scale"></param>
        /// <param name="ang"></param>
        /// <param name="pL"></param>
        public static void AddDoubleListDimRotated(Database db, ref Extents2d ext,
          Point3d Pref, List<Point3d> npts, double scale, double ang = 0, int pL = 100, ArrowDirection dir = ArrowDirection.North, Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter)
        {

            RotatedDimension D1;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;

                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                //var dimID = dst[string.Format("1-{0:G}", scale)];
                Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
               {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
               };
                Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
                {
                   { Unit.Meter,"M" },
                   { Unit.Centimeter,"CM" },
                   { Unit.Millimeter,"MM" },
                 };
                double factor = UnitDic[plotunit] / UnitDic[paperunit];
                string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                var dimID = dst[DimStyleName];
                Point3d ptSt = new Point3d();
                Point3d ptEnd = new Point3d();
                if (npts.Count == 2)
                {
                    Point3d P1 = npts[0];
                    Point3d P2 = npts[1];
                    ptSt = P1;
                    ptEnd = P2;
                    D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, "", dimID);
                    D1.LayerId = lt["H标注"];
                   
                    modelSpace.AppendEntity(D1);
                    tr.AddNewlyCreatedDBObject(D1, true);
                    Polyline line = new Polyline();
                    line.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                    line.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                    line.AddVertexAt(1, ptSt.C2D(8 * scale, 8 * scale), 0, 0, 0);
                    line.AddVertexAt(2, ptEnd.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                    if (line.Bounds != null)
                        ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                }
                else if (npts.Count > 2 && npts.Count <= 6)
                {

                    for (int i = 0; i < npts.Count - 1; i++)
                    {
                        Point3d P1 = npts[i];
                        Point3d P2 = npts[i + 1];
                        if (i == 0)
                        {
                            ptSt = P1;
                        }
                        if (i == npts.Count - 2)
                        {
                            ptEnd = P2;
                        }
                        D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, "", dimID);
                        D1.LayerId = lt["H标注"];

                        modelSpace.AppendEntity(D1);
                        tr.AddNewlyCreatedDBObject(D1, true);
                        Polyline line1 = new Polyline();
                        line1.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                        line1.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                        line1.AddVertexAt(1, P1.C2D(8 * scale, 8 * scale), 0, 0, 0);
                        line1.AddVertexAt(2, P2.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                        if (line1.Bounds != null)
                            ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));
                    }
                    D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), ptSt, ptEnd, Pref.C3D(5 * scale, 5 * scale), "", dimID);
                    D1.LayerId = lt["H标注"];

                    modelSpace.AppendEntity(D1);
                    Polyline line = new Polyline();
                    line.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                    line.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                    line.AddVertexAt(1, ptSt.C2D(8 * scale, 8 * scale), 0, 0, 0);
                    line.AddVertexAt(2, ptEnd.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                    if (line.Bounds != null)
                        ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                    tr.AddNewlyCreatedDBObject(D1, true);
                }
                else if (npts.Count > 6)
                {
                    for (int i = 0; i < npts.Count - 1; i++)
                    {
                        if (i < 2 || i > npts.Count - 2 - 2)
                        {
                            Point3d P1 = npts[i];
                            Point3d P2 = npts[i + 1];
                            if (i == 0)
                            {
                                ptSt = P1;
                            }
                            if (i == npts.Count - 2)
                            {
                                ptEnd = P2;
                            }
                            D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, "", dimID);
                            D1.LayerId = lt["H标注"];

                            modelSpace.AppendEntity(D1);
                            tr.AddNewlyCreatedDBObject(D1, true);
                            Polyline line1 = new Polyline();
                            line1.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                            line1.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                            line1.AddVertexAt(1, P1.C2D(8 * scale, 8 * scale), 0, 0, 0);
                            line1.AddVertexAt(2, P2.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                            if (line1.Bounds != null)
                                ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));
                        }
                        else if (i == 2)
                        {
                            Point3d P1 = npts[2];
                            Point3d P2 = npts[npts.Count - 1 - 2];
                            D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, "", dimID);
                            D1.LayerId = lt["H标注"];

                          
                            modelSpace.AppendEntity(D1);
                            if (pL > 1)
                            {
                                string replaceText = (D1.Measurement /( pL*factor)) + "×" + (pL* factor);
                                D1.DimensionText = replaceText;
                              
                            }
                            tr.AddNewlyCreatedDBObject(D1, true);
                            Polyline line1 = new Polyline();
                            line1.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                            line1.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                            line1.AddVertexAt(1, P1.C2D(8 * scale, 8 * scale), 0, 0, 0);
                            line1.AddVertexAt(2, P2.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                            if (line1.Bounds != null)
                                ext = ext.Add(new Extents2d(line1.Bounds.Value.MinPoint.C2D(), line1.Bounds.Value.MaxPoint.C2D()));
                        }
                    }
                    double Offset = 5 * scale;
                    switch (dir)
                    {
                        case ArrowDirection.North: //上
                            Offset = 5 * scale;
                            break;
                        case ArrowDirection.South: // 下
                            Offset = -5 * scale;
                            break;
                        case ArrowDirection.East:  // 右
                            Offset = 5 * scale;
                            break;
                        case ArrowDirection.West:  //  左
                            Offset = -5 * scale;
                            break;
                    }
                    D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), ptSt, ptEnd, Pref.C3D(Offset, Offset), "", dimID);
                    D1.LayerId = lt["H标注"];

                    modelSpace.AppendEntity(D1);
                    Polyline line = new Polyline();
                    line.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                    line.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                    line.AddVertexAt(1, ptSt.C2D(8 * scale, 8 * scale), 0, 0, 0);
                    line.AddVertexAt(2, ptEnd.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                    if (line.Bounds != null)
                        ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                    tr.AddNewlyCreatedDBObject(D1, true);
                }
                tr.Commit();
            }

        }


        /// <summary>
        /// 添加标注
        /// </summary>
        /// <param name="P1"></param>
        /// <param name="P2"></param>
        /// <param name="Pref"></param>
        /// <param name="dimID"></param>
        /// <param name="ang"></param>
        /// <param name="replaceText"></param>
        /// <returns></returns>
        public static RotatedDimension AddRotDim(Database db, ref Extents2d ext, Point3d P1, Point3d P2, Point3d Pref
            , double scale, double ang = 0, Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter, string replaceText = "", string D = "")
        {
            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                //var dimID = dst[string.Format("1-{0:G}", scale)];
                Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
               {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
               };
                Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
                {
                   { Unit.Meter,"M" },
                   { Unit.Centimeter,"CM" },
                   { Unit.Millimeter,"MM" },
                 };
                double factor = UnitDic[plotunit] / UnitDic[paperunit];
                string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                var dimID = dst[DimStyleName];
     
                RotatedDimension D1 = new RotatedDimension(GeTools.DegreeToRadian(ang), P1, P2, Pref, D + replaceText, dimID);
                D1.LayerId = lt["H标注"];
                //double d = D1.Measurement;
                //Console.WriteLine(d);
                //Polyline line = new Polyline();
                //line.AddVertexAt(0, Pref.C2D(8 * scale, 8 * scale), 0, 0, 0);
                //line.AddVertexAt(0, Pref.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                //line.AddVertexAt(1, P1.C2D(8 * scale, 8 * scale), 0, 0, 0);
                //line.AddVertexAt(2, P2.C2D(-8 * scale, -8 * scale), 0, 0, 0);
                //if (line.Bounds != null)
                //    ext = ext.Add(new Extents2d(line.Bounds.Value.MinPoint.C2D(), line.Bounds.Value.MaxPoint.C2D()));
                modelSpace.AppendEntity(D1);
                tr.AddNewlyCreatedDBObject(D1, true);

                ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));
                tr.Commit();
                return D1;
            }

        }

        /// <summary>
        /// 水平标注
        /// </summary>
        /// <param name="db"></param>
        /// <param name="P1">起点</param>
        /// <param name="P2">终点</param>
        /// <param name="Pref">标注位置</param>
        /// <param name="dimID">标注样式id</param>
        /// <param name="ang">转角，弧度</param>
        /// <returns></returns>
        public static RotatedDimension DimRotated(Database db, ref Extents2d ext, Point3d P1, Point3d P2, Point3d Pref
            , double ang = 0, double scale = 20, Unit plotunit = Unit.Millimeter, Unit paperunit = Unit.Millimeter, string replaceText = "", string D = "", int pL = 1)
        {
            RotatedDimension D1;

            using (Transaction tr = db.TransactionManager.StartTransaction())
            {
                BlockTable blockTbl = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable;
                BlockTableRecord modelSpace = tr.GetObject(blockTbl[BlockTableRecord.ModelSpace],
                    OpenMode.ForWrite) as BlockTableRecord;
                DimStyleTable dst = (DimStyleTable)tr.GetObject(db.DimStyleTableId, OpenMode.ForRead);
                LayerTable lt = tr.GetObject(db.LayerTableId, OpenMode.ForRead) as LayerTable;
                //var dimID = dst[string.Format("1-{0:G}", scale)];
                Dictionary<Unit, double> UnitDic = new Dictionary<Unit, double>()
               {
                { Unit.Meter,1000 },
                { Unit.Centimeter,10 },
                { Unit.Millimeter,1 },
               };
                Dictionary<Unit, string> UnitDicStr = new Dictionary<Unit, string>()
                {
                   { Unit.Meter,"M" },
                   { Unit.Centimeter,"CM" },
                   { Unit.Millimeter,"MM" },
                 };
                double factor = UnitDic[plotunit] / UnitDic[paperunit];
                string DimStyleName = string.Format("{0}-{1}-", UnitDicStr[plotunit], UnitDicStr[paperunit]);
                if (scale < 1)
                {
                    DimStyleName += Math.Round(1 / scale, 0).ToString() + "-1";
                }
                else
                {
                    DimStyleName += "1-" + scale.ToString();
                }
                var dimID = dst[DimStyleName];
                D1 = new RotatedDimension(GeTools.DegreeToRadian(ang)/*ang*/, P1, P2, Pref, replaceText, dimID);
                D1.LayerId = lt["H标注"];
                if (pL > 1)
                {
                    replaceText = (D1.Measurement/ pL) + "×" + pL;
                    D1.DimensionText = replaceText;
                   
                }
                else
                {
                    if (string.IsNullOrWhiteSpace(replaceText))
                    {                   
                            replaceText = (Math.Round(D1.Measurement , 1)).ToString();
                            D1.DimensionText = D + replaceText;
                       
                    }
                }
                modelSpace.AppendEntity(D1);
                tr.AddNewlyCreatedDBObject(D1, true);

                    ext = ext.Add(new Extents2d(D1.Bounds.Value.MinPoint.C2D(), D1.Bounds.Value.MaxPoint.C2D()));

                tr.Commit();
            }
            return D1;

        }

    }
}


